package com.aplink.dellcommercial.configuration;

import com.aplink.dellcommercial.R;
import com.aplink.dellcommercial.activities.LoginActivity;
import com.aplink.dellcommercial.constant.Constant;
import com.aplink.dellcommercial.database.sharepreference.CustomerSharePreferenceHelper;
import com.aplink.dellcommercial.util.DellCommercialUtility;
import com.aplink.generic.controller.AppGenericController;
import com.aplink.generic.controller.SessionManager;

public class DellController extends AppGenericController {

    private volatile static DellController instance = null;
    private volatile static boolean mIsOnline = false;

    private String mAccountToken;
    private String mExpired_server;
    private String mExpired_local;
    private String mContactID;
    private String mAccountEmail;

    public static DellController getInstance() {
        return instance;
    }

    public static boolean isOnline() {
        return mIsOnline;
    }

    public static synchronized void setOnline( boolean isOnline ) {
        mIsOnline = isOnline;
    }

    public DellController() {
        super();
        instance = this;
    }

    public String getAccountId() {
        return this.getContactID();
    }

    public String getAccountToken() {
        return this.mAccountToken;
    }

    public void setCheckLog( boolean isCheckLog ) {
        CustomerSharePreferenceHelper.storeBoolean( Constant.IS_CHECK_LOG,
                isCheckLog );
    }

    public String getExpiredServer() {
        if ( mExpired_server == null ) {
            mExpired_server = CustomerSharePreferenceHelper
                    .getString( Constant.token_ExpriedServer );
        }
        return mExpired_server;
    }

    public String getContactID() {
        if ( this.mContactID == null ) {
            this.mContactID = CustomerSharePreferenceHelper
                    .getString( Constant.CONTACT_ID );
        }
        return this.mContactID;
    }

    public String getJsonAccountToken() {
        if ( this.mAccountToken == null ) {
            this.mAccountToken = CustomerSharePreferenceHelper
                    .getString( Constant.ACCOUNT_TOKEN );
        }
        return String.format( "{\"token\":\"%s\"}", this.mAccountToken );
    }

    @Override
    public void initProject() {
        SessionManager.setInstance( instance.getApplicationContext(),
                LoginActivity.class, DellParameter.DATABASE_NAME,
                R.raw.table_structures, R.raw.table_datas );
    }

    @Override
    public void onCreate() {
        super.onCreate();
        DellCommercialUtility.checkConnect( this );
    }

    public void setAccountToken( String token ) {
        this.mAccountToken = token;
        CustomerSharePreferenceHelper
                .storeString( Constant.ACCOUNT_TOKEN, token );
    }

    public void setAccountEmail( String email ) {
        mAccountEmail = email;
        CustomerSharePreferenceHelper
                .storeString( Constant.ACCOUNT_EMAIL, email );
    }

    public String getExpiredLocal() {
        if ( mExpired_local == null ) {
            mExpired_local = CustomerSharePreferenceHelper
                    .getString( Constant.token_ExpriedLocal );
        }
        return mExpired_local;
    }

    public void setExpiredLocal( String expiredLocal ) {
        mExpired_local = expiredLocal;
        CustomerSharePreferenceHelper.storeString( Constant.token_ExpriedLocal,
                expiredLocal );
    }

    public void setExpiredServer( String expiredServer ) {
        mExpired_server = expiredServer;
        CustomerSharePreferenceHelper.storeString( Constant.token_ExpriedServer,
                expiredServer );
    }

    public String getAccountEmail() {
        if ( mAccountEmail == null ) {
            mAccountEmail = CustomerSharePreferenceHelper
                    .getString( Constant.ACCOUNT_EMAIL );
        }
        return mAccountEmail;
    }

    public void setContactID( String contactID ) {
        this.mContactID = contactID;
        CustomerSharePreferenceHelper.storeString( Constant.CONTACT_ID,
                contactID );
    }

    public void setLogin( boolean isLogin ) {
        CustomerSharePreferenceHelper.storeBoolean( Constant.IS_LOGIN, isLogin );
    }

    public boolean getLogin() {
        return CustomerSharePreferenceHelper.getBoolean( Constant.IS_LOGIN );
    }
}
